/**
 *
 */
package anysware.restlet.resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.restlet.data.CharacterSet;
import org.restlet.data.MediaType;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.Variant;

import anysware.restlet.gsp.TemplateRepresentation;

/**
 * TODO: Doc me
 *
 * @author keke <keke@codehaus.org>
 * @version
 * @revision $Revision:$
 */
public class GWTHostPageResource extends Resource {
	private Map<String, Object>	binding;
	private String	            templateName;

	@Override
	public List<Variant> getVariants() {
		final List<Variant> varList = super.getVariants();
		final Variant variant = new Variant(MediaType.TEXT_HTML);
		variant.setCharacterSet(CharacterSet.UTF_8);
		varList.add(variant);
		return varList;
	}

	@Override
	public Representation represent(final Variant variant)
	        throws ResourceException {
		if (binding == null) {
			binding = new HashMap<String, Object>();
		}
		binding.put("agent", getRequest().getClientInfo().getAgent());
		return new TemplateRepresentation(Thread.currentThread()
		        .getContextClassLoader().getResourceAsStream(templateName),
		        binding, variant.getMediaType());
	}

	public void setBinding(final Map<String, Object> context) {
		binding = context;
	}

	public void setTemplateName(final String templateName) {
		this.templateName = templateName;
	}

}
